﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using RTGS.Library.Entities.UserManagement;
using RTGS.Library.DataAccess.UserManagement;
using System.Configuration;

namespace RTGS.Web.form.usermanagement
{
    public partial class sys_t_menuform : BasePage
    {

        private int id = -1;
        private sysTMenuRepo menuDal = new sysTMenuRepo(ConfigurationManager.ConnectionStrings["Rtgs"].ConnectionString);
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Params["Id"] != null)
            {
                id = Convert.ToInt32(Request.Params["Id"]);
            }

            if (!Page.IsPostBack)
            {
                List<sysTMenu> menus = menuDal.GetHierarchyList("");
                ddlParent.Items.Clear();
                menus.ForEach(delegate(sysTMenu menu)
                {
                    ddlParent.Items.Add(new ListItem(menu.menuName, menu.menuId.ToString()));
                });

                ddlParent.Items.Insert(0, new ListItem("-- select menu parent --", "-1"));

                if (id > 0) // Edit an existing item
                {
                    sysTMenu menu = menuDal.GetById(id);
                    if (menu != null)
                    {
                        hfID.Value = id.ToString();
                        txtName.Text = menu.menuName;
                        txtUrl.Text = menu.url;

                        txtOrderSeq.Text = (menu.orderSeq.HasValue) ? menu.orderSeq.Value.ToString() : string.Empty;

                        if (menu.parentId.HasValue)
                        {
                            ddlParent.ClearSelection();
                            ddlParent.Items.FindByValue(menu.parentId.Value.ToString()).Selected = true;                        
                        }
                    }
                }
            }

        }
    
    }
}
